//
//  RootViewController.m
//  Blog
//
//  Created by zou leeging on 10-12-12.
//  Copyright 2010 apple. All rights reserved.
//

#import "RootViewController.h"
#import "XMLReader.h"
#import "WebViewController.h"
#import "Reachability.h"
#import "AdMobView.h"

@implementation RootViewController
@synthesize blogArray;


- (void)viewDidLoad {
	[super viewDidLoad];
	
	
	if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) &&
		([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
		
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"注意" 
													   message:@"无可用网络连接" 
													  delegate:self 
											 cancelButtonTitle:@"知道了" 
											 otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	self.navigationItem.title = @"韩寒博客";
    
	blogArray = [[NSArray alloc]init];
	self.tableView.rowHeight = 55;
	//-------------------
	CGRect appFrame = [UIScreen mainScreen].applicationFrame;
	
	self.tableView.frame = appFrame;
	self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
	// Request an ad
	adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
	[adMobAd retain]; // this will be released when it loads (or fails to load)
	
	
	//-------------------

	self.view.backgroundColor = [UIColor colorWithRed:0 green:0.52 blue:0.83 alpha:0.20];
	[self.tableView setBackgroundColor:[UIColor lightGrayColor]];
	self.tableView.separatorColor= [UIColor grayColor];
	
    XMLReader * reader = [[XMLReader alloc]init];
	reader.delegate = self;
	
	[reader getBlogs];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)XMLReader:(XMLReader *)reader didGetBlogs:(NSArray *)blogs {
	
	self.blogArray = [blogs retain];
	[self.tableView reloadData];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

//----------

- (NSString *)publisherIdForAd:(AdMobView *)adView {
	return @"a14d04b52124e98"; // this should be prefilled; if not, get it from www.admob.com
}

- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView {
	return self;
}

- (UIColor *)adBackgroundColorForAd:(AdMobView *)adView {
	return [UIColor colorWithRed:0.498 green:0.565 blue:0.667 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)primaryTextColorForAd:(AdMobView *)adView {
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)secondaryTextColorForAd:(AdMobView *)adView {
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (void)didReceiveAd:(AdMobView *)adView {
	NSLog(@"AdMob: Did receive ad");
	// get the view frame
	CGRect frame = self.tableView.frame;
	
	// put the ad at the bottom of the screen
	adMobAd.frame = CGRectMake(0, frame.size.height - 48, frame.size.width, 48);
	
	[self.tableView addSubview:adMobAd];
}

// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView {
	NSLog(@"AdMob: Did fail to receive ad");
	[adMobAd removeFromSuperview];  // Not necessary since never added to a view, but doesn't hurt and is good practice
	[adMobAd release];
	adMobAd = nil;
	// we could start a new ad request here, but in the interests of the user's battery life, let's not
}



#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [blogArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		
		UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 310, 30)]; 
		[titleLabel setTag:100];
		titleLabel.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
		[titleLabel setTag:100];
		titleLabel.backgroundColor = [UIColor clearColor];
		[cell.contentView addSubview:titleLabel];
		[titleLabel release];
		
		UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 32, 210, 18)];
		[dateLabel setTag:200];
		dateLabel.textColor = [UIColor darkGrayColor];
		dateLabel.font = [UIFont systemFontOfSize:15];
		[dateLabel setTag:200];
		dateLabel.backgroundColor = [UIColor clearColor];
		[cell.contentView addSubview:dateLabel];
		[dateLabel release];
		
		
	}
    
    // Configure the cell...
	Blog *log = [self.blogArray objectAtIndex:indexPath.row];
	
	UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:100];
	UILabel *dateLabel = (UILabel *)[cell.contentView viewWithTag:200];
	titleLabel.text = log.title;
	dateLabel.text = log.creatAt;
	    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //HanBlogAppDelegate *appDelegate = (HanBlogAppDelegate *)[[UIApplication sharedApplication]delegate];
	Blog *bg = [blogArray objectAtIndex:indexPath.row];
	
	NSString *html = bg.content;
	NSString *blogTitle = bg.title;
	//NSLog(@"html = %@",[bg content]);
	
	WebViewController *web_controller = [[WebViewController alloc]init];
	web_controller.htmlString = html;
	web_controller.blogTitle = blogTitle;
	
	[self.navigationController pushViewController:web_controller animated:YES];
	//web_controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	
	//[self.navigationController pushViewController:web_controller animated:YES];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[blogArray release];
    [super dealloc];
}


@end

